<!DOCTYPE html>


<html lang="zn-CH">


<head>
  <meta charset="utf-8" />
    
  <meta name="description" content="What I can do is to Do My Personal Best!" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    &#39;python之excel读写报表统计入门&#39; |  小熊的博客
  </title>
  <meta name="generator" content="hexo-theme-yilia-plus">
  
  <link rel="shortcut icon" href="http://www.cdn.adingxiong.com/b5363a06dc744d79a94021492bcefe27" />
  
  
<link rel="stylesheet" href="/blog/dist/main.css">

  
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">

  
<link rel="stylesheet" href="/blog/css/custom.css">

  
  
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

  
  

  

<link rel="alternate" href="/blog/atom.xml" title="小熊的博客" type="application/atom+xml">
</head>

</html>

<body>
  <div id="app">
    <main class="content on">
      <section class="outer">
  <article id="post-python之excel读写报表统计入门" class="article article-type-post" itemscope
  itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  &#39;python之excel读写报表统计入门&#39;
</h1>
 

    </header>
    

    
    <div class="article-meta">
      <a href="/blog/2021/02/19/python%E4%B9%8Bexcel%E8%AF%BB%E5%86%99%E6%8A%A5%E8%A1%A8%E7%BB%9F%E8%AE%A1%E5%85%A5%E9%97%A8/" class="article-date">
  <time datetime="2021-02-19T05:48:06.000Z" itemprop="datePublished">2021-02-19</time>
</a>
      
      
      
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> Word count:</span>
            <span class="post-count">1.6k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> Reading time≈</span>
            <span class="post-count">7 min</span>
        </span>
    </span>
</div>

      
    </div>
    

    
    
    <div class="tocbot"></div>





    

    
    <div class="article-entry" itemprop="articleBody">
      
      

      
      <h2 id="1-环境准备"><a href="#1-环境准备" class="headerlink" title="1.  环境准备"></a>1.  环境准备</h2><ul>
<li><code>python3.0+</code>,<a href="https://repo.huaweicloud.com/python/" target="_blank" rel="noopener">下载地址</a></li>
<li>环境变量配置,将<code>Python</code>安装的路径添加值系统环境变量的<code>path</code>中,如下图所示<br><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/6710216c43ff4cfdbde3fc9d096c246d~tplv-k3u1fbpfcp-watermark.image" alt></li>
<li>检测环境变量,<code>window</code>使用快捷键<code>win+r</code>输入<code>cmd</code>然后输入<code>python</code>,环境配置正确如下显示<br><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/9a7fc08575ef4e2f94852886a5e9cb47~tplv-k3u1fbpfcp-watermark.image" alt></li>
</ul>
<h2 id="2-依赖相关"><a href="#2-依赖相关" class="headerlink" title="2. 依赖相关"></a>2. 依赖相关</h2><p>随着<code>python</code>版本升级, 版本在2.7以上的,在安装<code>Python</code>的时候,已经自动安装好了<code>pip</code>.<code>pip</code>是下载相关依赖的引擎,每个人的理解不一样.相当于<code>java</code>中的<code>mvn</code>,也相当于<code>node</code>中的<code>npm</code>,可以使用<code>pip install 模块名</code> 的方式下载所需的依赖.</p>
<p>比如我们这里学习使用<code>python</code>自动化处理<code>excel</code>的时候,会需要引用模块<code>openpyxl</code>,<a href="https://openpyxl.readthedocs.io/en/stable/charts/introduction.html" target="_blank" rel="noopener">API等操作文档</a>. 按照开发思路,第一步是需要引用这个依赖的  </p>
<h3 id="2-1-依赖下载"><a href="#2-1-依赖下载" class="headerlink" title="2.1 依赖下载"></a>2.1 依赖下载</h3><ol>
<li>在<code>cmd</code>或者<code>vscode</code>编辑器的终端中  输入<code>pip install openpyxl</code>,在刚安装好<code>Python</code>环境去执行这个命令的时候,可能会出现两种结果.</li>
</ol>
<ul>
<li>报错提示需要升级<code>pip</code>版本,根<code>node</code>不同的地方是,如果<code>pip</code>不是最新版本将会无法拉去模块文件,所以我们需要去升级<code>pip</code>模块</li>
<li>如果<code>pip</code>是新版本的话,你拉去模块的时候进度条加载慢或者过程中出现<code>Timeout</code>字样.这是因为模块文件比较大,默认拉去的是<code>python</code>官网地址的模块文件,所以我们需要中转站(镜像)</li>
</ul>
<h3 id="2-2-镜像使用"><a href="#2-2-镜像使用" class="headerlink" title="2.2 镜像使用"></a>2.2 镜像使用</h3><p>可以参考我的第一篇文章 <a href="https://juejin.cn/post/6867419027937951752" target="_blank" rel="noopener">python入门</a><br>里面有关于镜像比较详细的说明,以及镜像的使用方法  </p>
<h2 id="3-excel操作相关"><a href="#3-excel操作相关" class="headerlink" title="3. excel操作相关"></a>3. <code>excel</code>操作相关</h2><h3 id="3-1-读excel报表数据"><a href="#3-1-读excel报表数据" class="headerlink" title="3.1 读excel报表数据"></a>3.1 读<code>excel</code>报表数据</h3><p>废话不多说,直接上代码.关于<code>openpyxl</code>的操作文档,上文已经写了.遇到问题或者有新创意可以参考官方文档.<br>假设需要读取的<code>excel</code>如下<br><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/57cc6d94a58444b1875d8a49ce360ce3~tplv-k3u1fbpfcp-watermark.image" alt></p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 引入所需的依赖   </span></span><br><span class="line"><span class="keyword">import</span> openpyxl</span><br><span class="line"><span class="comment"># 读取excel文件,方便操作建议放在和python文件同目录,或者使用绝的路径的方式,需要注意盘符</span></span><br><span class="line">wb = openpyxl.load_workbook(<span class="string">'helloword.xlsx'</span>)</span><br><span class="line"><span class="comment"># 获取该文件下所有sheet页 ,获取的api有多种</span></span><br><span class="line">names = wb.sheetnames</span><br><span class="line"><span class="comment"># wb.get_sheet_by_name(name) 已经废弃,使用wb[name] 获取指定工作表</span></span><br><span class="line">sheet = wb[names[<span class="number">0</span>]]</span><br><span class="line"><span class="comment"># 获取最大行数</span></span><br><span class="line">maxRow = sheet.max_row</span><br><span class="line"><span class="comment"># 获取最大列数</span></span><br><span class="line">maxColumn = sheet.max_column</span><br><span class="line"><span class="comment"># 获取当前活动表</span></span><br><span class="line">current_sheet = wb.active</span><br><span class="line"><span class="comment"># 获取当前活动表名称</span></span><br><span class="line">current_name = sheet.title</span><br><span class="line"><span class="comment"># 打印sheet数组</span></span><br><span class="line">print(names)</span><br><span class="line">---------------------------------------------------------------------------------</span><br><span class="line">PS D:\python-workplace&gt; &amp; D:/python/python.exe d:/python-workplace/excel-demo1.py</span><br><span class="line">[<span class="string">'Sheet1'</span>, <span class="string">'测试'</span>, <span class="string">'活动页'</span>]</span><br><span class="line">---------------------------------------------------------------------------------</span><br><span class="line"><span class="comment"># 获取指定的sheet页 对象</span></span><br><span class="line">sh1 = wb[<span class="string">'Sheet1'</span>]</span><br><span class="line"><span class="comment"># 读取指定单元格值 </span></span><br><span class="line">cell_v = sh1[<span class="string">'A1'</span>].value</span><br><span class="line"><span class="comment"># 通过行列确定数据 </span></span><br><span class="line">cell_RL = sh1.cell(row=<span class="number">1</span>,column=<span class="number">2</span>).value</span><br><span class="line">print(<span class="string">"读取指定单元格值: &#123;&#125;"</span> .format(cell_v)</span><br><span class="line">print(<span class="string">"通过行列读取指定数据:&#123;&#125;"</span>.format(cell_RL))</span><br><span class="line">---------------------------------------------------------------------------------</span><br><span class="line">PS D:\python-workplace&gt; &amp; D:/python/python.exe d:/python-workplace/excel-demo1.py</span><br><span class="line">读取指定单元格值: Pie</span><br><span class="line">通过行列读取指定数据:sold</span><br><span class="line">---------------------------------------------------------------------------------</span><br><span class="line"><span class="comment"># 获取指定类 和指定行的数据</span></span><br><span class="line">col_range = ws[<span class="string">'A:B'</span>]</span><br><span class="line">row_range = ws[<span class="string">'1:5'</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment">#  按照列读(一列一列)</span></span><br><span class="line"><span class="keyword">for</span> col <span class="keyword">in</span> col_range:</span><br><span class="line">    <span class="keyword">for</span> cell <span class="keyword">in</span> col:</span><br><span class="line">        print(cell.value)</span><br><span class="line">        </span><br><span class="line">---------------------------------------------------------------------------------</span><br><span class="line">PS D:\python-workplace&gt; &amp; D:/python/python.exe d:/python-workplace/excel-demo1.py</span><br><span class="line">Pie</span><br><span class="line">apple</span><br><span class="line">balana</span><br><span class="line">Pumkin</span><br><span class="line">orange</span><br><span class="line">sold</span><br><span class="line"><span class="number">50</span></span><br><span class="line"><span class="number">30</span></span><br><span class="line"><span class="number">10</span></span><br><span class="line"><span class="number">10</span></span><br><span class="line">---------------------------------------------------------------------------------</span><br><span class="line"></span><br><span class="line"><span class="comment"># 按照行读 (一行一行)</span></span><br><span class="line"><span class="keyword">for</span> row <span class="keyword">in</span> row_range:</span><br><span class="line">    <span class="keyword">for</span> cell <span class="keyword">in</span> row:</span><br><span class="line">        print(cell.value)</span><br><span class="line">---------------------------------------------------------------------------------</span><br><span class="line">PS D:\python-workplace&gt; &amp; D:/python/python.exe d:/python-workplace/excel-demo1.py</span><br><span class="line">Pie</span><br><span class="line">sold</span><br><span class="line">apple</span><br><span class="line"><span class="number">50</span></span><br><span class="line">balana</span><br><span class="line"><span class="number">30</span></span><br><span class="line">Pumkin</span><br><span class="line"><span class="number">10</span></span><br><span class="line">orange</span><br><span class="line"><span class="number">10</span></span><br><span class="line">---------------------------------------------------------------------------------</span><br><span class="line"></span><br><span class="line"><span class="comment"># 合并上面 </span></span><br><span class="line"><span class="keyword">for</span> row <span class="keyword">in</span> ws.iter_rows(min_row=<span class="number">1</span>,max_row=<span class="number">5</span>,max_col=<span class="number">2</span>):</span><br><span class="line">    <span class="keyword">for</span> cell <span class="keyword">in</span> row:</span><br><span class="line">        print(cell.value)</span><br><span class="line">---------------------------------------------------------------------------------</span><br><span class="line">PS D:\python-workplace&gt; &amp; D:/python/python.exe d:/python-workplace/excel-demo1.py</span><br><span class="line">Pie</span><br><span class="line">sold</span><br><span class="line">apple</span><br><span class="line"><span class="number">50</span></span><br><span class="line">balana</span><br><span class="line"><span class="number">30</span></span><br><span class="line">Pumkin</span><br><span class="line"><span class="number">10</span></span><br><span class="line">orange</span><br><span class="line"><span class="number">10</span></span><br><span class="line">---------------------------------------------------------------------------------</span><br></pre></td></tr></table></figure>


<h3 id="3-2-写入数据到excel"><a href="#3-2-写入数据到excel" class="headerlink" title="3.2 写入数据到excel"></a>3.2 写入数据到<code>excel</code></h3><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> openpyxl</span><br><span class="line"><span class="keyword">from</span> openpyxl.utils <span class="keyword">import</span> get_column_letter</span><br><span class="line"></span><br><span class="line"><span class="string">""" wb = openpyxl.Workbook()</span></span><br><span class="line"><span class="string">sheet = wb.active</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">print(sheet.title)</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">#  修改标题名称 </span></span><br><span class="line"><span class="string">sheet.title ="修改sheet名称"</span></span><br><span class="line"><span class="string">print(wb.get_sheet_names())</span></span><br><span class="line"><span class="string"># 文件保存到磁盘 </span></span><br><span class="line"><span class="string">#wb.save("excel_writer.xlsx")</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"># 按照指定顺序新建sheet页  </span></span><br><span class="line"><span class="string">wb.create_sheet(index=0,title='First Sheet')</span></span><br><span class="line"><span class="string">wb.create_sheet(index=1,title='Second Sheet')</span></span><br><span class="line"><span class="string">print(wb.get_sheet_names()) """</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 往excel单元个写入数据 </span></span><br><span class="line">wb = openpyxl.Workbook()</span><br><span class="line">sheet = wb.active</span><br><span class="line"></span><br><span class="line"><span class="comment"># 指定单元列填充数值</span></span><br><span class="line">sheet[<span class="string">'A1'</span>] = <span class="string">"hello python 2021"</span></span><br><span class="line">print(sheet[<span class="string">'A1'</span>].value)</span><br><span class="line"><span class="comment"># 行数据填充  </span></span><br><span class="line">ws = wb.create_sheet(<span class="string">'range rows'</span>)</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">1</span>,<span class="number">39</span>):</span><br><span class="line">    ws.append(range(<span class="number">17</span>))</span><br><span class="line"></span><br><span class="line">ws1 = wb.create_sheet(<span class="string">'List'</span>)</span><br><span class="line">rows = [</span><br><span class="line">    [<span class="string">'Number'</span>,<span class="string">'Batch 1'</span>,<span class="string">'Batch2'</span>],</span><br><span class="line">    [<span class="number">2</span>,<span class="number">100</span>,<span class="number">500</span>],</span><br><span class="line">    [<span class="number">3</span>,<span class="number">200</span>,<span class="number">11</span>],</span><br><span class="line">    [<span class="number">4</span>,<span class="number">12</span>,<span class="number">777</span>],</span><br><span class="line">    [<span class="number">5</span>,<span class="number">22</span>,<span class="number">666</span>],</span><br><span class="line">    [<span class="number">6</span>,<span class="number">543</span>,<span class="number">544</span>],</span><br><span class="line">]</span><br><span class="line"><span class="keyword">for</span> row <span class="keyword">in</span> rows:</span><br><span class="line">   ws1.append(row) </span><br><span class="line"></span><br><span class="line">ws2 = wb.create_sheet(<span class="string">"Date"</span>)</span><br><span class="line"><span class="comment"># 按照行列的方式填充数值</span></span><br><span class="line"><span class="keyword">for</span> row <span class="keyword">in</span> range(<span class="number">5</span>,<span class="number">30</span>):</span><br><span class="line">    <span class="keyword">for</span> col <span class="keyword">in</span> range(<span class="number">10</span>,<span class="number">45</span>):</span><br><span class="line">        ws2.cell(column=col,row=row,value=get_column_letter(col))</span><br><span class="line"></span><br><span class="line">print(ws2[<span class="string">'AA10'</span>])</span><br><span class="line"><span class="comment"># 保存到磁盘</span></span><br><span class="line">wb.save(<span class="string">'excel_writer.xlsx'</span>)</span><br><span class="line"><span class="comment"># 读取sheet  </span></span><br><span class="line">sts = wb.get_sheet_names()</span><br><span class="line">print(sts)</span><br><span class="line"></span><br><span class="line">sh1 = wb.get_sheet_by_name(<span class="string">'Sheet'</span>)</span><br><span class="line">print(sh1[<span class="string">'A1'</span>].value)</span><br><span class="line"></span><br><span class="line">sh2 = wb.get_sheet_by_name(<span class="string">'range rows'</span>)</span><br><span class="line">print(sh2.rows)</span><br></pre></td></tr></table></figure>
<h3 id="3-3-excel字体-颜色-宽高-跨行跨列-简单图表等设置"><a href="#3-3-excel字体-颜色-宽高-跨行跨列-简单图表等设置" class="headerlink" title="3.3 excel字体,颜色,宽高,跨行跨列,简单图表等设置"></a>3.3 <code>excel</code>字体,颜色,宽高,跨行跨列,简单图表等设置</h3><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">import</span> openpyxl</span><br><span class="line"><span class="keyword">from</span> openpyxl.styles <span class="keyword">import</span> Font</span><br><span class="line"><span class="keyword">from</span> openpyxl.styles <span class="keyword">import</span> colors</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># excel 操作相关的  字体 样式 大小 等等的修改 </span></span><br><span class="line"></span><br><span class="line">wb = openpyxl.Workbook()</span><br><span class="line"></span><br><span class="line"><span class="comment"># font  相关  </span></span><br><span class="line">ws = wb.active</span><br><span class="line">ws.title=<span class="string">'Font'</span></span><br><span class="line"><span class="comment"># 设置字体 24px  斜体</span></span><br><span class="line">italic24Font = Font(size=<span class="number">24</span>,italic=<span class="literal">True</span>)</span><br><span class="line">ws[<span class="string">'B3'</span>].font = italic24Font</span><br><span class="line">ws[<span class="string">'B3'</span>] = <span class="string">'24 px itailc '</span></span><br><span class="line"><span class="comment"># 设置字体 Times New Roman 加粗 颜色为红色</span></span><br><span class="line">boldRedFont = Font(name=<span class="string">'Times New Roman'</span>,bold=<span class="literal">True</span>,color=colors.COLOR_INDEX[<span class="number">2</span>])</span><br><span class="line">ws[<span class="string">'A1'</span>].font = boldRedFont</span><br><span class="line">ws[<span class="string">'A1'</span>] = <span class="string">'bold red font'</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#  Formula  公式  求和等 </span></span><br><span class="line">ws = wb.create_sheet(<span class="string">'Formula'</span>)</span><br><span class="line">ws[<span class="string">'A1'</span>] = <span class="number">200</span></span><br><span class="line">ws[<span class="string">'A2'</span>] =<span class="number">300</span></span><br><span class="line">ws[<span class="string">'A3'</span>] =<span class="string">'=SUM(A1:A2)'</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 高宽  设置行列或者单元格的高宽</span></span><br><span class="line">ws = wb.create_sheet(<span class="string">'dimensions'</span>)</span><br><span class="line">ws[<span class="string">'A1'</span>] =  <span class="string">'Tall row'</span></span><br><span class="line">ws.row_dimensions[<span class="number">1</span>].height = <span class="number">70</span></span><br><span class="line">ws[<span class="string">'B2'</span>] = <span class="string">'Width column'</span></span><br><span class="line">ws.column_dimensions[<span class="string">'B'</span>].Width = <span class="number">20</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 单元格合并  </span></span><br><span class="line">ws = wb.create_sheet(<span class="string">'megred'</span>)</span><br><span class="line">ws.merge_cells(<span class="string">'A1:D3'</span>)</span><br><span class="line">ws[<span class="string">'A1'</span>] = <span class="string">'Twelve cells megred together'</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 拆分单元格 </span></span><br><span class="line">ws = wb.copy_worksheet(wb[<span class="string">'megred'</span>])</span><br><span class="line">ws.title =<span class="string">'unmegerd'</span></span><br><span class="line">ws.unmerge_cells(<span class="string">'A1:D3'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 图标操作   绘制图标  </span></span><br><span class="line"><span class="keyword">from</span> openpyxl.chart <span class="keyword">import</span>(</span><br><span class="line">    AreaChart,</span><br><span class="line">    Reference,</span><br><span class="line">    Series,</span><br><span class="line">    PieChart,</span><br><span class="line">)</span><br><span class="line"><span class="keyword">from</span> openpyxl.chart.label <span class="keyword">import</span> DataLabelList</span><br><span class="line"><span class="keyword">from</span> openpyxl.chart.text <span class="keyword">import</span> RichText</span><br><span class="line"><span class="keyword">from</span> openpyxl.drawing.text <span class="keyword">import</span> (</span><br><span class="line">    Paragraph, </span><br><span class="line">    ParagraphProperties, </span><br><span class="line">    CharacterProperties</span><br><span class="line">)</span><br><span class="line">ws = wb.create_sheet(<span class="string">'AreaChart'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">data = [</span><br><span class="line">    [<span class="string">'Pie'</span>, <span class="string">'sold'</span>],</span><br><span class="line">    [<span class="string">'apple'</span>,<span class="number">50</span>],</span><br><span class="line">    [<span class="string">'balana'</span>,<span class="number">30</span>],</span><br><span class="line">    [<span class="string">'Pumkin'</span>,<span class="number">10</span>],</span><br><span class="line">    [<span class="string">'orange'</span>,<span class="number">10</span>]</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> row <span class="keyword">in</span> data:</span><br><span class="line">    ws.append(row)</span><br><span class="line"><span class="comment"># 创建折线图实例</span></span><br><span class="line">pie = AreaChart()</span><br><span class="line"><span class="comment"># 设置标题</span></span><br><span class="line">pie.title=<span class="string">'pie sold chart'</span></span><br><span class="line"><span class="comment"># 统计图样式 参加官网</span></span><br><span class="line">pie.style =<span class="number">13</span></span><br><span class="line"><span class="comment"># x周标题</span></span><br><span class="line">pie.x_axis.title =<span class="string">'Test'</span></span><br><span class="line"><span class="comment"># y轴标题</span></span><br><span class="line">pie.y_axis.title =<span class="string">'Percentage'</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建统计项</span></span><br><span class="line">cats = Reference(ws,min_col=<span class="number">1</span>,min_row=<span class="number">2</span>,max_row=<span class="number">6</span>)</span><br><span class="line"><span class="comment"># 添加数据</span></span><br><span class="line">datas = Reference(ws,min_col=<span class="number">2</span>,min_row=<span class="number">1</span>,max_row=<span class="number">5</span>)</span><br><span class="line"><span class="comment"># 插入数据并从表中读取标题</span></span><br><span class="line">pie.add_data(datas,titles_from_data=<span class="literal">True</span>)</span><br><span class="line">pie.set_categories(cats)</span><br><span class="line"><span class="comment"># 插入指定位置</span></span><br><span class="line">ws.add_chart(pie,<span class="string">"A10"</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">p = PieChart()</span><br><span class="line">p.title=<span class="string">'水果销售统计'</span></span><br><span class="line">p.add_data(datas,titles_from_data=<span class="literal">True</span>)</span><br><span class="line">p.set_categories(cats)</span><br><span class="line">p.style =<span class="number">10</span> </span><br><span class="line">p.height =<span class="number">10</span></span><br><span class="line"><span class="comment"># 获取数据实例</span></span><br><span class="line">s1 = p.series[<span class="number">0</span>]</span><br><span class="line"><span class="comment"># 获取数据并设置百分比</span></span><br><span class="line">s1.dLbls = DataLabelList()</span><br><span class="line">s1.dLbls.showCatName = <span class="literal">True</span>     <span class="comment"># 标签显示</span></span><br><span class="line">s1.dLbls.showVal = <span class="literal">False</span>     <span class="comment"># 数量显示</span></span><br><span class="line">s1.dLbls.showPercent = <span class="literal">True</span>     <span class="comment"># 百分比显示</span></span><br><span class="line">axis = CharacterProperties(sz=<span class="number">1200</span>)     <span class="comment"># 图表中字体大小 *100</span></span><br><span class="line">s1.dLbls.txPr = RichText(p=[Paragraph(pPr=ParagraphProperties(defRPr=axis), endParaRPr=axis)])</span><br><span class="line">ws.add_chart(p,<span class="string">'J10'</span>)</span><br><span class="line">wb.save(<span class="string">'excel_style.xlsx'</span>)</span><br></pre></td></tr></table></figure>
<p><img src="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/44fe63d537844a29b45148eab3a3e42a~tplv-k3u1fbpfcp-watermark.image" alt></p>
<p><img src="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/1dab7a964a754a68a10e3af622ac1e1d~tplv-k3u1fbpfcp-watermark.image" alt></p>

      
      <!-- reward -->
      
      <div id="reward-btn">
        Donate
      </div>
      
    </div>
    
    
      <!-- copyright -->
      
        <div class="declare">
          <ul class="post-copyright">
            <li>
              <i class="ri-copyright-line"></i>
              <strong>Copyright： </strong>
              Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
            </li>
          </ul>
        </div>
        
    <footer class="article-footer">
      
          
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=https://bodboy.gitee.io/blog/2021/02/19/python%E4%B9%8Bexcel%E8%AF%BB%E5%86%99%E6%8A%A5%E8%A1%A8%E7%BB%9F%E8%AE%A1%E5%85%A5%E9%97%A8/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog/tags/excel/" rel="tag">excel</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog/tags/python/" rel="tag">python</a></li></ul>


    </footer>

  </div>

  
  
  <nav class="article-nav">
    
    
      <a href="/blog/2021/01/14/Linux%E6%93%8D%E4%BD%9C%E6%8C%87%E4%BB%A4%E9%9A%8F%E8%AE%B0/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">Linux操作指令随记</div>
      </a>
    
  </nav>


  

  
  
<!-- valine评论 -->
<div id="vcomments-box">
    <div id="vcomments">
    </div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='https://cdn.jsdelivr.net/npm/valine@1.3.10/dist/Valine.min.js'></script>
<script>
    new Valine({
        el: '#vcomments',
        app_id: 'uW8QYGvvhWmNWdiwpznJ0GVa-gzGzoHsz',
        app_key: 'LrJhMzx9qEwcMV1DJjTI6f6C',
        path: window.location.pathname,
        notify: 'false',
        verify: 'false',
        avatar: '[object Object]',
        placeholder: '给我的文章加点评论吧~',
        recordIP: true
    });
    const infoEle = document.querySelector('#vcomments .info');
    if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
        infoEle.childNodes.forEach(function (item) {
            item.parentNode.removeChild(item);
        });
    }
</script>
<style>
    #vcomments-box {
        padding: 5px 30px;
    }

    @media screen and (max-width: 800px) {
        #vcomments-box {
            padding: 5px 0px;
        }
    }

    #vcomments-box #vcomments {
        background-color: #fff;
    }

    .v .vlist .vcard .vh {
        padding-right: 20px;
    }

    .v .vlist .vcard {
        padding-left: 10px;
    }
</style>

  

  
  
  
  
  

</article>
</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2019-2021
        <i class="ri-heart-fill heart_icon"></i> panda
      </li>
    </ul>
    <ul>
      <li>
        
        
        
        Powered by <a href="https://hexo.io" target="_blank">Hexo</a>
        <span class="division">|</span>
        Theme - <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>Visitors:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>Views:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/blog/"><img src="https://portrait.gitee.com/uploads/avatars/user/1007/3023420_bodboy_1578973210.png!avatar200" alt="小熊的博客"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/blog/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/blog/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/blog/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/blog/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/blog/tags/%E6%97%85%E8%A1%8C/">旅行</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/blog/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/blog/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/blog/images/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/blog/images/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/blog/js/jquery-2.0.3.min.js"></script>


<script src="/blog/js/lazyload.min.js"></script>


<script>
  try {
    var typed = new Typed("#subtitle", {
      strings: ['面朝大海，春暖花开', '愿你一生努力，一生被爱', '想要的都拥有，得不到的都释怀'],
      startDelay: 0,
      typeSpeed: 200,
      loop: true,
      backSpeed: 100,
      showCursor: true
    });
  } catch (err) {
  }

</script>




<script src="/blog/js/tocbot.min.js"></script>

<script>
  // Tocbot_v4.7.0  http://tscanlin.github.io/tocbot/
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>



<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/blog/dist/main.js"></script>



<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>


<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
      tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
  });

  MathJax.Hub.Queue(function() {
      var all = MathJax.Hub.getAllJax(), i;
      for(i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
      }
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.6/unpacked/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script>
  var ayerConfig = {
    mathjax: true
  }
</script>




<script src="/blog/js/busuanzi-2.3.pure.min.js"></script>



<script type="text/javascript" src="https://js.users.51.la/20544303.js"></script>

  
<script src="/blog/js/clickLove.js"></script>



<!-- 复制 -->



    
    <div id="music">
    
    
    
    <iframe frameborder="no" border="1" marginwidth="0" marginheight="0" width="200" height="52"
        src="//music.163.com/outchain/player?type=2&id=1297802566&auto=1&height=32"></iframe>
</div>

<style>
    #music {
        position: fixed;
        right: 15px;
        bottom: 0;
        z-index: 998;
    }
</style>
    
  </div>
</body>

</html>